﻿using Enyim.Caching;
using Enyim.Caching.Memcached;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace conMemcache
{

    class Program
    {
        static void Main(string[] args)
        {

            MemcachedClient client = new MemcachedClient();

            //-- 新增或更新，存在key, 则覆盖
            client.ExecuteStore(StoreMode.Set, "test-set-key1", "whatever set 1");
            //-- 新增或更新，存在key, 则覆盖: 指定有效期 1 小时
            client.ExecuteStore(StoreMode.Set, "test-set-key2", "whatever set 2", DateTime.Now.AddHours(1));


            //-- 新增， 存在该key, 则不覆盖
            client.ExecuteStore(StoreMode.Add, "test-add-key1", "whatever add 1");

            //-- 更新， 不存在该key, 则不做更新
            client.ExecuteStore(StoreMode.Replace, "test-replace-key1", "whatever replace key1");


            //-- 取值
            client.Get<string>("test-set-key1"); // whatever set 1
            client.Get<string>("test-add-key1"); // whatever add 1


            //-- 缓存对象: 对象必须可序列化

            Foo foo = new Foo { Id = 1, Name = "foo1" };
            client.ExecuteStore(StoreMode.Set, "obj1", foo, DateTime.Now.AddHours(1));

            //取值
            var cacheFoo = client.Get<Foo>("obj1");

        }
    }

    [Serializable]
    public class Foo
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

}
